package cn.lyw.init;

import cn.common.init.InitDataConstant;
import cn.common.redis.RedisServie;
import cn.common.restresult.util.RestResult;
import cn.common.restresult.util.ResultEnum;
import cn.lyw.discount.service.bo.EditAdminDiscountBO;
import cn.lyw.discount.service.domain.GetAdminDiscountByIdDO;
import cn.lyw.discount.service.service.AdminDiscountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author lyw
 * @Description 监听redis过期key
 * @Date 12:04 2021/7/20
 * @Param
 * @return
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {

    public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
        super(listenerContainer);
    }


    @Autowired
    private AdminDiscountService adminDiscountService;

    @Autowired
    private RedisServie redisServie;

    /**
     * @return
     * @Author lyw
     * @Description 针对 redis 数据失效事件，进行数据处理
     * @Date 12:04 2021/7/20
     * @Param
     */
    @Override
    public void onMessage(Message message, byte[] pattern) {

        // 获取到失效的 key
        String expiredKey = message.toString();
        //通过：截取字符串
        String[] split = expiredKey.split(":");
        System.out.println(expiredKey);

        //如果过期的KEY 是未开始的，就将它数据库的状态改为活动中，并添加到redis中
        if (split[1].equals("notStart")) {

            //获取修改前的优惠券信息
            GetAdminDiscountByIdDO adminDiscountById1 = adminDiscountService.getAdminDiscountById(split[2]);

            //修改优惠券状态
            EditAdminDiscountBO editAdminDiscountBO = new EditAdminDiscountBO();
            editAdminDiscountBO.setDiscountId(split[2]);
            editAdminDiscountBO.setDiscountState("activity");
            editAdminDiscountBO.setVersion(adminDiscountById1.getVersion());
            RestResult restResult = adminDiscountService.editAdminDiscount(editAdminDiscountBO);
            if (restResult.getCode().equals(ResultEnum.EDIT_ERROR.code)) {
                System.out.println("修改失败了");
            }

            //获取到修改后的优惠券
            GetAdminDiscountByIdDO adminDiscountById2 = adminDiscountService.getAdminDiscountById(split[2]);

            //获取时间
            long time1 = adminDiscountById2.getDiscountEnd().getTime();
            //当前时间
            long time2 = (new Date()).getTime();
            long l = (time1 - time2) / 1000;

            //存入redis
            redisServie.putHash(InitDataConstant.DISCOUNT_ACTIVITY + adminDiscountById2.getDiscountId(), adminDiscountById2.getDiscountId(), adminDiscountById2, l);
            //存入活动中的优惠券
            redisServie.putHash(InitDataConstant.DISCOUNT_ACTIVITY_KEY, adminDiscountById2.getDiscountId(), adminDiscountById2);

            System.out.println("执行成功");
        }


        //如果过期的KEY 是活动中的，就将它数据库的状态改为已过期
        if (split[1].equals("activity")) {

            //获取修改前的优惠券信息
            GetAdminDiscountByIdDO adminDiscountById1 = adminDiscountService.getAdminDiscountById(split[2]);

            //修改优惠券状态
            EditAdminDiscountBO editAdminDiscountBO = new EditAdminDiscountBO();
            editAdminDiscountBO.setDiscountId(split[2]);
            editAdminDiscountBO.setDiscountState("finished");
            editAdminDiscountBO.setVersion(adminDiscountById1.getVersion());
            RestResult restResult = adminDiscountService.editAdminDiscount(editAdminDiscountBO);
            if (restResult.getCode().equals(ResultEnum.EDIT_ERROR.code)) {
                System.out.println("修改失败了");
            }

            //删除redis中数据
            redisServie.delHash(InitDataConstant.DISCOUNT_ACTIVITY_KEY, adminDiscountById1.getDiscountId());
            System.out.println("执行成功");

        }


    }

}
